package com.qf.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/10/13 10:55
 */
@SuppressWarnings("all")
@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    DtsAdminMapper dtsAdminMapper;
    @Autowired
    DtsBrandMapper brandMapper;

    @Override
    public DtsAdmin findByUserName(String username) {

        QueryWrapper<DtsAdmin> queryWrapper = new QueryWrapper<>();

        QueryWrapper<DtsAdmin> wrapper = queryWrapper.eq("username", username);

        DtsAdmin dtsAdmin = dtsAdminMapper.selectOne(wrapper);

        return dtsAdmin;
    }

    /**
     * 分页查找
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String username) {
//        分页对象
        IPage<DtsAdmin> iPage = new Page<>(page,limit);
//        条件
        QueryWrapper<DtsAdmin> wrapper = new QueryWrapper<>();
        //如果名字为空，就不查询，如果传参，就带条件查询
        if(!StringUtils.isEmpty(username)){
            wrapper.eq("username", username);
        }
        //排序条件
        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }

        wrapper.eq("deleted", 0);

        dtsAdminMapper.selectPage(iPage,wrapper);

        //获取当页展示的数据
        List<DtsAdmin> records = iPage.getRecords();

        records.stream().map(dtsAdmin -> {
            dtsAdmin.setPassword("");
            return dtsAdmin;
        }).collect(Collectors.toList());

        //获取条数
        long total = iPage.getTotal();

        Map<String,Object> data = new HashMap<>();
        data.put("total",total);
        data.put("items",records);

        return data;
    }

    @Override
    public void insertAdmin(DtsAdmin dtsAdmin) {
        dtsAdmin.setAddTime(new Date());
        dtsAdmin.setUpdateTime(new Date());
        dtsAdmin.setDeleted(false);

        //设置密码为密文
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(dtsAdmin.getPassword());

        dtsAdmin.setPassword(encode);
        dtsAdminMapper.insert(dtsAdmin);
    }

    @Override
    public void updateAdmin(DtsAdmin dtsAdmin) {
        //修改时间
        dtsAdmin.setUpdateTime(new Date());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(dtsAdmin.getPassword());
        //加密修改的密码
        dtsAdmin.setPassword(encode);

        dtsAdminMapper.updateById(dtsAdmin);


    }

    @Override
    public void deleteAdmin(DtsAdmin dtsAdmin) {
//        首先判断admin 是否被其他表所关联
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(DtsBrand::getAdminId, dtsAdmin.getId());
        wrapper.eq(DtsBrand::getDeleted,false);

        Integer integer = brandMapper.selectCount(wrapper);
        //如果查询到就说明绑定就不可以删除,需要捕获捕获异常并返回前端
        if( integer>0 ){
            throw new RuntimeException("该管理员被品牌所关联，不可以删除");
        }

        dtsAdmin.setDeleted(true);
        dtsAdminMapper.updateById(dtsAdmin);
    }

    @Override
    public List<Map<String, Object>> findAll() {

        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getDeleted,false);

        List<DtsAdmin> dtsAdmins = dtsAdminMapper.selectList(wrapper);

        List<Map<String, Object>> collect = dtsAdmins.stream().map(dtsAdmin -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", dtsAdmin.getId());
            map.put("label", dtsAdmin.getUsername());
            return map;
        }).collect(Collectors.toList());

        return collect;
    }
}
